package com.danger.Activity.Util;

import java.util.Map;

import com.android.volley.AuthFailureError;
import com.android.volley.Request.Method;
import com.android.volley.Response;
import com.android.volley.Response.Listener;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.danger.Activity.MyApplication;

import android.content.Context;
import android.util.Log;

public class LoadJson {

    private Context context;
    private Map<String, String> map;

    public LoadJson(Context context) {
        this(context, null);
        this.context = context;
    }

    public LoadJson(Context context, Map<String, String> map) {
        this.context = context;
        this.map = map;
    }

    public void getJSONByPost(String url, final OnLoadJsonListener listener) {
        StringRequest request = new StringRequest(Method.POST, url, new Listener<String>() {

            @Override
            public void onResponse(String arg0) {
                Log.i("TAG", "success" + arg0);
                listener.onSuccess(arg0);
            }
        }, new Response.ErrorListener() {

            @Override
            public void onErrorResponse(VolleyError arg0) {
                Log.i("TAG", "error" + arg0);
                listener.onError(arg0.getMessage());
            }
        }) {
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                return map;
            }
        };
        request.setTag("abcPost");
        MyApplication.getRequestQueue().add(request);
    }

    public void getJSONByGet(String url, final OnLoadJsonListener listener) {
        StringRequest request = new StringRequest(Method.GET, url, new Listener<String>() {
            @Override
            public void onResponse(String arg0) {
                Log.i("TAG", "success" + arg0);
                listener.onSuccess(arg0);
            }
        }, new Response.ErrorListener() {

            @Override
            public void onErrorResponse(VolleyError arg0) {
                Log.i("TAG", "error" + arg0);
                listener.onError(arg0.getMessage());
            }
        });
        request.setTag("abcGet");
        MyApplication.getRequestQueue().add(request);
    }
}
